#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@file app.py
@author weiximei
@date 2025/2/28 10:13
"""
from dataclasses import dataclass
from typing import Optional

"""
@dataclass 它通过自动为类生成特殊方法如__init__、__repr__、__eq__等，简化了类的创建过程
"""


@dataclass
class DatabaseConfig:
    """
    数据库

    如果存在 url，则直接返回 url 链接：
    mysql+pymysql://root:123456@lolcahost:3306/yqm_tdesgin

    否则：
        mysql+pymysql://{username}:{password}@{host}:{port}/{database}
    """

    # 数据类型：mysql
    type: str
    url: Optional[str] = ''
    username: Optional[str] = ''
    password: Optional[str] = ''
    host: Optional[str] = ''
    port: Optional[str] = ''
    database_name: Optional[str] = ''

    def get_connection_url(self) -> str:
        """获取数据库连接URL"""
        if self.url is not None and self.url != '':
            return self.url
        return f"mysql+pymysql://{self.username}:{self.password}@{self.host}:{self.port}/{self.database_name}"


@dataclass
class RedisConfig:
    host: str
    port: int
    db: Optional[int] = 0


@dataclass
class YqmConfig:
    name: str
    version: str


@dataclass
class AppConfig:
    database: Optional[DatabaseConfig] = None
    redis: Optional[RedisConfig] = None
    yqm: Optional[YqmConfig] = None
